package com.wpw.dream.dubbo.rest.service.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.rpc.RpcContext;
import com.wpw.dream.dubbo.rest.entry.User;
import com.wpw.dream.dubbo.rest.service.RestService;

@Service("restService")
public class RestServiceImpl implements RestService {
	
	
	@Override
	public Object param(Long id) {
		/*Map<String, Long> resMap = new HashMap<>();
		resMap.put("id", id);
		return JSON.toJSONString(resMap);*/
		User user = new User(id, "测试", "男");
		return user;
	}
	
	
	@Override
	public Object returnJson(Long id) {
		User user = new User(id, "测试", "男");
		return user;
	}

	
	@Override
	public User returnXml(Long id) {
		User user = new User(id, "测试", "男");
		return user;
	}
	
	
	@Override
	public Object paramJson(User user) {
		System.out.println(user);
		return user;
	}
	
	
	@Override
	public Object paramXml(User user) {
		System.out.println(user);
		return user;
	}
	
	
	@Override
	public Object getConntext(HttpServletRequest request) {
		 return request.getRemoteAddr();
	}
	
	@Override
	public Object getRpcConntext() {
		
		if (RpcContext.getContext().getRequest(HttpServletRequest.class) != null) {
		    System.out.println("Client address is " + RpcContext.getContext().getRequest(HttpServletRequest.class).getRemoteAddr());
		}

		if (RpcContext.getContext().getResponse(HttpServletResponse.class) != null) {
		    System.out.println("Response object from RpcContext: " + RpcContext.getContext().getResponse(HttpServletResponse.class));
		}
		
		return RpcContext.getContext().getRemoteAddressString();
	}
	
	

}
